package sqlite_storage

import (
	"go-iot-gateway/internal/apk/maths"
	"go-iot-gateway/internal/conf_table/define"
	storage_define "go-iot-gateway/internal/storage/define"
)

// 最新值数据
type LatestVal struct {
	Id        int64   //主键自增
	TagName   string  `xorm:"tag_name"`
	StrVal    string  `xorm:"str_val"`
	NumVal    float64 `xorm:"num_val"`
	LastVal   string  `xorm:"last_val"`
	IsFix     bool    `xorm:"is_fix"`
	Ts        int64   `xorm:"ts"`
	Qos       uint8   `xorm:"qos"`
	DataType  string  `xorm:"data_type"`
	ChannelId string  `xorm:"channel_id"`
	DeviceId  string  `xorm:"device_id"`
	UpdatedAt int64   `xorm:"updated_at"`
}

func (receiver *LatestVal) TableName() string {
	return storage_define.LATEST_TABLE_NAME
}

func (latestVal *LatestVal) ToValObject() storage_define.ValObject {
	if latestVal.DataType == conf_table_define.DATA_TYPE_STRING {
		return storage_define.NewStringVal(latestVal.TagName,
			latestVal.StrVal, latestVal.Ts, latestVal.Qos)
	}

	return storage_define.NewFloat64Val(latestVal.TagName, maths.Turn4Float(latestVal.NumVal),
		latestVal.Ts, latestVal.Qos)

	//switch latestVal.DataType {
	//case conf_table_define.DATA_TYPE_FLOAT64, conf_table_define.DATA_TYPE_FLOAT32:
	//	return storage_define.NewFloat64Val(latestVal.TagName, latestVal.NumVal,
	//		latestVal.Ts, latestVal.Qos)
	//}
	//return storage_define.NewInt64Val(latestVal.TagName, int64(latestVal.NumVal),
	//	latestVal.Ts, latestVal.Qos)
}
